﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SquareMaxSum
{
    class MaxPlatform
    {
        static void Main(string[] args)
        {
            int[,] matrix = {
              { 7, 1, 3, 6, 2, 1 },
              { 2, 6, 1, 8, 3, 4 },
              { 2, 5, 3, 7, 6, 1 },
              { 2, 5, 3, 7, 6, 1 },
              { 2, 5, 3, 7, 6, 1 },
            };

            int bestSum = int.MinValue;
            int sum;
            int rowMaxSum = 0;
            int colMaxSum = 0;

            for (int rows = 0; rows < matrix.GetLength(0)-2; rows++)
            {
                for (int cols = 0; cols < matrix.GetLength(1)-2; cols++)
                {
                    sum = matrix[rows, cols] + matrix[rows, cols + 1] + matrix[rows, cols + 2] + matrix[rows + 1, cols]
                        + matrix[rows + 1, cols + 1] + matrix[rows + 1, cols + 2] + matrix[rows + 2, cols] + matrix[rows + 2, cols + 1]
                        + matrix[rows + 2, cols + 2];
                    if (sum > bestSum)
                    {
                        bestSum = sum;
                        rowMaxSum = rows;
                        colMaxSum = cols;
                    }
                    sum = 0;
                }
            }

            // Print the Matrix

            for (int rows = 0; rows < 3; rows++)
            {
                for (int cols = 0; cols < 3; cols++)
                {
                    Console.Write("{0,3} ", matrix[rowMaxSum, colMaxSum]);
                    colMaxSum++;
                }
                colMaxSum -= 3;
                rowMaxSum++;
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("The Maximum sum is: {0}", bestSum);
        }
    }
}
